package com.yksj.healthtalk.ui.views;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.ui.R;

public class DoctorInfoBtnView extends LinearLayout implements OnClickListener {

	private ArrayList<HashMap<String, String>> list;
	private int btnCount = 0;
	private OnClickDoctorServiceButtonListener listener;
	private ViewGroup v1;
	private Context context;
	private ImageView line;

	public DoctorInfoBtnView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public DoctorInfoBtnView(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		if (list != null) {
			list.clear();
		}
		this.context = context;
		removeAllViews();
		this.setOrientation(VERTICAL);
		if (context instanceof OnClickDoctorServiceButtonListener) {
			this.listener = (OnClickDoctorServiceButtonListener) context;
		}
		ViewGroup v = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.doctor_info_btn, null);
		for (int i = 0; i < v.getChildCount(); i++) {
			TextView tv = (TextView) v.getChildAt(i);
			tv.setId(i + 1);
			tv.setOnClickListener(this);
		}
		addView(v);
		line = new ImageView(context);
		line.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,1));
		line.setBackgroundResource(R.drawable.line3);
		v1 = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.doctor_info_btn, null);
		for (int i = 0; i < v1.getChildCount(); i++) {
			TextView tv = (TextView) v1.getChildAt(i);
			tv.setId(i + 5);
			tv.setOnClickListener(this);
		}
		addView(line);
		addView(v1);
		this.setVisibility(View.GONE);
	}

	public void setServiceType(String str) {
		this.setVisibility(View.VISIBLE);
		list = new ArrayList<HashMap<String, String>>();
		try {
			Object object = JSON.parse(str);
			if (object instanceof JSONArray) {
				JSONArray array = (JSONArray) object;
				for (int i = 0; i < array.size(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					JSONObject o = array.getJSONObject(i);
					String service_type_id = o.getString("SERVICE_TYPE_ID");
					String service_type = o.getString("SERVICE_TYPE");
					map.put("SERVICE_TYPE_ID", service_type_id);
					map.put("SERVICE_TYPE", service_type);
					list.add(map);
				}
			}
		} catch (Exception e) {
		}
		btnCount = btnCount + list.size();
		// 设置服务类型的值
		if (list.size() > 7) {
			return;
		}
	/*	for (int i = 0; i < list.size(); i++) {
			((TextView) findViewById(i + 1)).setText(list.get(i).get("SERVICE_TYPE"));
		}*/

		// 按钮小于4个
		if (btnCount < 5) {
			v1.setVisibility(View.GONE);
			line.setVisibility(View.GONE);
		}

		switch (btnCount) {
		case 1:
			findViewById(2).setVisibility(View.INVISIBLE);
			findViewById(3).setVisibility(View.INVISIBLE);
			findViewById(4).setVisibility(View.INVISIBLE);
			break;
		case 2:
			findViewById(3).setVisibility(View.INVISIBLE);
			findViewById(4).setVisibility(View.INVISIBLE);
			break;
		case 3:
			findViewById(4).setVisibility(View.INVISIBLE);
			break;
		case 5:
			findViewById(6).setVisibility(View.INVISIBLE);
			findViewById(7).setVisibility(View.INVISIBLE);
			findViewById(8).setVisibility(View.INVISIBLE);
			break;
		case 6:
			findViewById(7).setVisibility(View.INVISIBLE);
			findViewById(8).setVisibility(View.INVISIBLE);
			break;
		case 7:
			findViewById(8).setVisibility(View.INVISIBLE);
			break;
		default:
			break;
		}

		
		//初始化图标
		for (int i = 0; i < list.size(); i++) {
			Integer j = Integer.valueOf(list.get(i).get("SERVICE_TYPE_ID"));
			TextView tv = (TextView)findViewById(i+1);
			Drawable img_off = null;
			String name = "";
//			Drawable img_left = null;
//			if (i != 0 && i != 4) {
//				img_left = 	context.getResources().getDrawable(R.drawable.adr_doctor_service_seperare_line);
//				img_left.setBounds(0, 0, img_left.getMinimumWidth(), img_left.getMinimumHeight());
//			}else {
//				img_left = null;
//			}
			if (j==1) {
				img_off = context.getResources().getDrawable(R.drawable.yuyue_mint_icon);
				name = "VIP服务";
			}else if (j==2){
				img_off = context.getResources().getDrawable(R.drawable.common_online);
				name = "网上咨询";
			}else if (j==3){
				img_off = context.getResources().getDrawable(R.drawable.yuyue_online);
				name = "门诊预约";
			}else if (j==-1){
				img_off = context.getResources().getDrawable(R.drawable.zhuti_salon);
				name = "医生话题";
			}else if (j== -2){
				img_off = context.getResources().getDrawable(R.drawable.message_logo);
				name = "医生发布";
			}else if (j==-3){
				img_off = context.getResources().getDrawable(R.drawable.service_content_icon);
				name = "服务介绍";
			}else if(j == -4){
				img_off = context.getResources().getDrawable(R.drawable.patient_guide);
				name = "患者指南";
			}else {
				img_off = new BitmapDrawable();
			}
//			tv.setImageDrawable(img_off);
			tv.setText(name);
			tv.setCompoundDrawablesWithIntrinsicBounds(null, img_off, null, null); //设置上图标
		}
	}

	@Override
	public void onClick(View v) {
		int size = list.size();
		if (v.getId() <= size) {
			Integer i = Integer.valueOf(list.get(v.getId() - 1).get("SERVICE_TYPE_ID"));
			switch (i) {
			case -1:
				if (listener != null) {
					listener.onClickProfessionalSalon();
				}
				break;
			case 1:// 1为普通服务，2为预约时段服务,3为预约面访服务
				if (listener != null) {
					listener.onClickCommonService();
				}
				break;
			case 2:
				if (listener != null) {
					listener.onClickTimeService();
				}
				break;
			case 3:
				if (listener != null) {
					listener.onClickInterviewService();
				}
				break;
			case -2://留言版
				if (listener != null) {
					listener.onClickMessage();
				}
				break;
			case -3://服务内容
				if (listener != null) {
					listener.onClickServiceContent();
				}
				break;
			case -4://患者指南
				if (listener != null) {
					listener.onClickPatientGuide();
				}
				break;
			default:
				break;
			}
		}
	}

	public interface OnClickDoctorServiceButtonListener {

		void onClickCommonService();

		void onClickInterviewService();

		void onClickTimeService();

		void onClickProfessionalSalon();

		void onClickCloseService();
		
		void onClickMessage();
		
		void onClickServiceContent();
		
		void onClickPatientGuide();
	}

}
